package com.youlai.boot.energyPlan.controller;

import com.youlai.boot.energyPlan.service.UserEnergyPlanService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.energyPlan.model.form.UserEnergyPlanForm;
import com.youlai.boot.energyPlan.model.query.UserEnergyPlanQuery;
import com.youlai.boot.energyPlan.model.vo.UserEnergyPlanVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 用户能量计划前端控制层
 *
 * @author cuishng
 * @since 2025-04-01 14:56
 */
@Tag(name = "用户能量计划接口")
@RestController
@RequestMapping("/api/v1/userEnergyPlans")
@RequiredArgsConstructor
public class UserEnergyPlanController  {

    private final UserEnergyPlanService userEnergyPlanService;

    @Operation(summary = "用户能量计划分页列表")
    @GetMapping("/page")
    public PageResult<UserEnergyPlanVO> getUserEnergyPlanPage(UserEnergyPlanQuery queryParams ) {
        IPage<UserEnergyPlanVO> result = userEnergyPlanService.getUserEnergyPlanPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增用户能量计划")
    @PostMapping
    public Result<Void> saveUserEnergyPlan(@RequestBody @Valid UserEnergyPlanForm formData ) {
        boolean result = userEnergyPlanService.saveUserEnergyPlan(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取用户能量计划表单数据")
    @GetMapping("/form")
    public Result<UserEnergyPlanForm> getUserEnergyPlanForm(
    ) {
        UserEnergyPlanForm formData = userEnergyPlanService.getUserEnergyPlanFormData();
        return Result.success(formData);
    }

    @Operation(summary = "修改用户能量计划")
    @PutMapping(value = "/{id}")
    public Result<Void> updateUserEnergyPlan(
            @Parameter(description = "用户能量计划ID") @PathVariable Long id,
            @RequestBody @Validated UserEnergyPlanForm formData
    ) {
        boolean result = userEnergyPlanService.updateUserEnergyPlan(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除用户能量计划")
    @DeleteMapping("/{ids}")
    public Result<Void> deleteUserEnergyPlans(
        @Parameter(description = "用户能量计划ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = userEnergyPlanService.deleteUserEnergyPlans(ids);
        return Result.judge(result);
    }
}
